package com.wl.jetpackdemo.banner

import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import com.wl.jetpackdemo.base.BaseActivity
import com.wl.jetpackdemo.base.BaseViewModel
import com.wl.jetpackdemo.databinding.ActivityBannersBinding
import com.wl.wllite.net.exception.ErrorStatus

/**
 * 展示列表Demo 带下拉刷新及加载更多
 */
class BannersActivity : BaseActivity() {
    private lateinit var binding: ActivityBannersBinding
    private lateinit var mAdapter: BannerAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityBannersBinding.inflate(layoutInflater)
        setContentView(binding.root)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        title = "列表页"

        binding.recyList.layoutManager = LinearLayoutManager(this)
        mAdapter = BannerAdapter()
        binding.recyList.adapter = mAdapter

        binding.refreshLayout.setOnRefreshListener(OnRefreshListener {
            getData(0)
        })

        mAdapter.loadMoreModule.setOnLoadMoreListener {
            getData(getViewModel().getPageIndex())
        }

        getViewModel().netBannerListData.observe(this, {
            if(binding.refreshLayout.isRefreshing)
                binding.refreshLayout.finishRefresh()
            if(it != null) {
                if(it.code == ErrorStatus.SUCCESS) {
                    val list = it.data
                    if(list != null) {
                        if(getViewModel().getPageIndex() == 0) {
                            mAdapter.setNewInstance(list as MutableList<Banner>?)
                        } else {
                            mAdapter.addData(list)
                        }

                        // 更新分页数据索引
                        getViewModel().setPageIndex(getViewModel().getPageIndex().plus(list.size))

                        //判断分页数据请求是否结束
                        if(getViewModel().getPageIndex() >= 30) {
                            mAdapter.loadMoreModule.loadMoreEnd(false)
                        } else {
                            mAdapter.loadMoreModule.loadMoreComplete()
                        }
                    }
                } else {
                    showShortToast( "${it.msg}(${it.code})");
                }
            }
        })

        getData(0)

        getViewModel().saveAndQueryUser()
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId) {
            android.R.id.home -> finish()
        }

        return super.onOptionsItemSelected(item)
    }

    private fun getData(page: Int) {
        Log.d("WLTest", "load pageIndex $page")
        if(page == 0) { //如果是下拉刷新，显示设置索引为0
            getViewModel().setPageIndex(0)
        }
        getViewModel().getBanners(page)
    }

    override fun generateViewModel(): BaseViewModel? {
        return ViewModelProvider(this).get(BannerViewModel::class.java)
    }

    private fun getViewModel() : BannerViewModel {
        return getBaseViewModel()
    }
}